

#include "board_dma.h"
volatile uint8_t end_of_transmit_flag = 0;

void init_dma(uint8_t *buffer, uint32_t buffer_size)
{
    DMA_InitTypeDef DMA_InitStructure;

    /* Turn on DMA1 module. */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    /* For UART1 we should use DMA1 CH4. */
    /* Clear init for DMA1 CH4. */
    DMA_DeInit(DMA1_Channel4);

    /* Set UART1 TX registr pointer. */
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART1->DR);
    /* Set memory buffer pointer. */
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)buffer;
    /* Set peripheral like destination. */
    /* Or direction from memory to peripheral.*/
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    /* Set size of memory buffer. */
    DMA_InitStructure.DMA_BufferSize = buffer_size;
    /* Disable incrementation of peripheral pointer. */
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    /* Enable incrementation of memory pointer. */
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    /* Set peripheral data size 8 bits. */
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    /* Set memory data size 8 bit. */
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    /* Set normal mode, means one time sending memory array to peripheral. */
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    /* Set up DMA priority. */
    DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
    /* Disable sending from memoty to memory. */
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    /* Init DMA. */
    DMA_Init(DMA1_Channel4, &DMA_InitStructure);

    /* Allow UART1 work with DMA. */
    USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);

    /* Init EndOfTransfer DMA interupt. */
    DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);


    /* Set up DMA interupt priority.*/
        NVIC_SetPriority(
                        DMA1_Channel4_IRQn,
                        NVIC_EncodePriority(
                                                NVIC_GetPriorityGrouping(),
                                                DMA1_Channel4_PRIORITY_GROUP,
                                                DMA1_Channel4_SUB_PRIORITY_GROUP
                                           )
                        );

    /* Turn on DMA interupt. */
    NVIC_EnableIRQ(DMA1_Channel4_IRQn);

/* Start or restart DMA. */
   // DMA_Cmd(DMA1_Channel4, DISABLE);
   // DMA_SetCurrDataCounter(DMA1_Channel4, sizeBufer);
   // DMA_Cmd(DMA1_Channel4, ENABLE);



}



void DMA1_Channel4_IRQHandler(void)
{
    DMA1->IFCR |= DMA_ISR_TCIF4;//clear end of transmit flag .
    end_of_transmit_flag = 1;
}

uint8_t GetStateDMAChannel4(void)
{
//  if(DMA1->ISR & DMA_ISR_TCIF4) return 1;   //end of transmit
    if(end_of_transmit_flag== 1)
    {
        return 1;        //end of transmit
    }
    end_of_transmit_flag = 0;
    return 0;                                 //on transmit
}











